package com.kh.pajx.sop.dao.sys.impl;

import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Repository;

import com.kh.pajx.sop.base.BaseDaoImpl;
import com.kh.pajx.sop.dao.sys.ISysDepartmentDao;
import com.kh.pajx.sop.domain.SysDepartment;

/**
 * 部门信息数据持久化操作实现类
 * @author:	azzcsimp
 * @Createdate:	2014年7月19日 上午2:13:54
 */
@Repository
@SuppressWarnings("unchecked")
public class SysDepartmentDaoImpl extends BaseDaoImpl<SysDepartment> implements ISysDepartmentDao {
	

	/**
	 * 查询所有顶级部门信息
	 * @see com.kh.pajx.sop.dao.sys.ISysDepartmentDao#findTopDepartment()
	 */
	@Override
	public List<SysDepartment> findTopDepartment() {
		return getSession().createQuery(//
				"FROM SysDepartment dept WHERE dept.parent IS NULL order by dept.deptCode")//
				.list();
	}

	/**
	 * 查询所有子级部门信息
	 * @see com.kh.pajx.sop.dao.sys.ISysDepartmentDao#findChildrenDepartment(java.lang.String)
	 */
	@Override
	public List<SysDepartment> findChildrenDepartment(String deptCode) {
		return getSession().createQuery(//
				"FROM SysDepartment dept WHERE dept.parent.deptCode=? order by dept.deptCode")//
				.setParameter(0, deptCode)//
				.list();
	}
	
	/**
	 * 获取当前最大值
	 * @see com.kh.pajx.sop.dao.sys.ISysDepartmentDao#getMaxDeptCode()
	 */
	@Override
	public String getMaxDeptCode() {
		List list = findListByHql("SELECT max(dept.deptCode) FROM SysDepartment dept ");
		if(null == list.get(0)){
			return "";
		}
		return list.get(0).toString();
	}
	
	/**
	 * 获取当前最大值
	 * @see com.kh.pajx.sop.dao.sys.ISysDepartmentDao#getMaxDeptCode(java.lang.String)
	 */
	@Override
	public String getMaxDeptCode(String deptCode) {
		List list = findListByHql(
				"SELECT max(dept.deptCode) FROM SysDepartment dept where dept.parent.deptCode=? ",
				deptCode);
		if (null == list.get(0)) {
			return "";
		}
		return list.get(0).toString();
	}
	
	@Override
	public List<SysDepartment> findAll() {
		return findList("FROM SysDepartment dept order by dept.deptCode asc");
	}
	
	/**
	 * 根据编码查询对象
	 * @see com.kh.pajx.sop.dao.sys.ISysDepartmentDao#getByCode(java.lang.String)
	 */
	public SysDepartment getByCode(String deptCode){
		return (SysDepartment) getSession().createQuery(//
				"FROM SysDepartment dept WHERE deptCode=?")//
				.setParameter(0, deptCode)//
				.uniqueResult();
	}
}
